package grsu.epam.airline.service;

import grsu.epam.airline.DAO.IUserDAO;
import grsu.epam.airline.DAOImpl.DAOFactory;
import grsu.epam.airline.constant.Constant;
import grsu.epam.airline.model.UserPrincipal;

import java.sql.SQLException;

public class UserService {
	public UserPrincipal getUser(String userName, String password) {
		IUserDAO userDao = (IUserDAO) DAOFactory.getInstance().getDAO(
				Constant.UserDAO);
		UserPrincipal user = null;

		try {
			user = (UserPrincipal) userDao.getUserByName(userName, password);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(userDao.getConnection());
		}
		return user;
	}

	public Boolean checkUser(Object user, String role) {
		Boolean isValid = false;
		if (((UserPrincipal) user).getRole().getRoleType().equals(role)) {
			isValid = true;
		}
		return isValid;
	}

}
